#include "frmmain.h"
#include "ui_frmmain.h"
#include "iconhelper.h"
#include "myhelper.h"
#include "myapp.h"
#include "selfselstocksetting.h"
#include "agencytransaction.h"
#include "miniwindowd.h"

int widgetIndex=0;

frmMain::frmMain(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::frmMain)
{
    ui->setupUi(this);

    //this->resize(400,300);
    //this->centralWidget();

    //db connection config
    //this->get_user_credentials();
    this->create_db_connection();
    //this->create_main_menu();

    SelfSelectW *selfSelectWidget=new SelfSelectW(this);
    IndividualStockW *individualStockWidget=new IndividualStockW(this);
    QuotesW *quotesWidget=new QuotesW(this);
    TradeW *tradeWidget=new TradeW(this);
    SelectStockW *selectStockWidget=new SelectStockW(this);
    InfoW *infoWidget=new InfoW(this);
    CircleW *circleWidget=new CircleW(this);
    DiscoverW *discoverWidget=new DiscoverW(this);

    //添加页面
    ui->stackedWidget->addWidget(selfSelectWidget);
    ui->stackedWidget->addWidget(individualStockWidget);
    ui->stackedWidget->addWidget(quotesWidget);
    ui->stackedWidget->addWidget(tradeWidget);
    ui->stackedWidget->addWidget(selectStockWidget);
    ui->stackedWidget->addWidget(infoWidget);
    ui->stackedWidget->addWidget(circleWidget);
    ui->stackedWidget->addWidget(discoverWidget);
    //显示页面作为主页
    ui->stackedWidget->setCurrentWidget(selfSelectWidget);

   // this->InitStyle();
    this->InitForm();
}

frmMain::~frmMain()
{
    delete ui;
}

void frmMain::InitStyle()
{
    this->setStyleSheet("QGroupBox#gboxMain{border-width:0px;}");
    this->setProperty("Form", true);
    //设置窗体标题栏隐藏--Qt::WindowStaysOnTopHint |
    this->setWindowFlags(Qt::FramelessWindowHint |
                         Qt::WindowSystemMenuHint |
                         Qt::WindowMinMaxButtonsHint);
}

void frmMain::change_style()
{
    QAction *action = (QAction *)sender();
    QString style = action->text();
    if (style == "淡蓝色") {
        myApp::AppStyle = ":/image/blue.css";
    } else if (style == "蓝色") {
        myApp::AppStyle = ":/image/dev.css";
    } else if (style == "灰色") {
        myApp::AppStyle = ":/image/gray.css";
    } else if (style == "黑色") {
        myApp::AppStyle = ":/image/black.css";
    } else if (style == "灰黑色") {
        myApp::AppStyle = ":/image/brown.css";
    } else if (style == "白色") {
        myApp::AppStyle = ":/image/white.css";
    } else if (style == "银色") {
        myApp::AppStyle = ":/image/silvery.css";
    }

    myHelper::SetStyle(myApp::AppStyle);
    myApp::WriteConfig();
}

void frmMain::InitForm()
{
    this->setWindowTitle(myApp::AppTitle);
}

void frmMain::create_db_connection() {
    ip = "localhost";
    port = "5432";

    db = QSqlDatabase::addDatabase("QPSQL");
    db.setHostName(ip);
    db.setPort(port.toInt());
    db.setDatabaseName("firebird_stock");
    db.setUserName("postgres");
    db.setPassword("");

    if(db.open()){
        qDebug() << "connected to db";
    } else {
        qDebug() << "can not connected to db";
        qDebug() << db.lastError();
        //prompt_error("An error occurred when connecting to database!\nCheck your login/password, or database status.",true);
    }
}

void frmMain::screen_full()
{
    this->setGeometry(qApp->desktop()->geometry());
    this->layout()->setContentsMargins(0, 0, 0, 0);
    //ui->widget_main->layout()->setContentsMargins(0, 0, 0, 0);

}

void frmMain::screen_normal()
{
    this->setGeometry(qApp->desktop()->availableGeometry());
    this->layout()->setContentsMargins(1, 1, 1, 1);
    //ui->widget_main->layout()->setContentsMargins(5, 5, 5, 5);
}

void frmMain::on_act_self_sel_stock_setting_triggered()
{
    qDebug()<<"自选股设置被按下";
    SelfSelStockSetting selfSelStockSetting;
    selfSelStockSetting.exec();
}

void frmMain::on_act_agency_transaction_triggered()
{
    qDebug()<<"委托交易被按下";
    AgencyTransaction agencyTransaction;
    agencyTransaction.exec();
}


void frmMain::on_act_zoom_triggered()
{
    screen_full();
}

void frmMain::on_act_quit_triggered()
{
    exit(0);
}

void frmMain::on_act_min_triggered()
{
    this->showMinimized();
}

void frmMain::on_btnSelfSelect_clicked()
{
    //ui->stackedWidget->setCurrentWidget(selfSelectWidget);
     ui->stackedWidget->setCurrentIndex(2);
}

void frmMain::on_btnIndividualStock_clicked()
{
    //ui->stackedWidget->setCurrentWidget(individualStockWidget);
     ui->stackedWidget->setCurrentIndex(3);
}

void frmMain::on_btnQuotes_clicked()
{
    //ui->stackedWidget->setCurrentWidget(quotesWidget);
     ui->stackedWidget->setCurrentIndex(4);
}

void frmMain::on_btnTrade_clicked()
{
    //ui->stackedWidget->setCurrentWidget(tradeWidget);
    ui->stackedWidget->setCurrentIndex(5);
}

void frmMain::on_btnSelectStock_clicked()
{
     ui->stackedWidget->setCurrentIndex(6);
   // ui->stackedWidget->setCurrentWidget(selectStockWidget);
}
void frmMain::on_btnInfo_clicked()
{
    //ui->stackedWidget->setCurrentWidget(infoWidget);
    ui->stackedWidget->setCurrentIndex(7);
}

void frmMain::on_btnCircle_clicked()
{
  //  ui->stackedWidget->setCurrentWidget(circleWidget);
    ui->stackedWidget->setCurrentIndex(8);
}

void frmMain::on_btnDiscover_clicked()
{
     ui->stackedWidget->setCurrentIndex(9);
   //ui->stackedWidget->setCurrentWidget(discoverWidget);
}



void frmMain::on_act_mini_window_triggered()
{
    qDebug()<<"迷你窗口被按下";
    MiniWindowD miniWindow;
    miniWindow.exec();
}
